package com.madou.yy.football;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.List;

public class Line extends JPanel{
    int startX = 0;
    int startY = 0;
    int endX = 0;
    int endY = 0;
    int stepX = 0;
    int stepY = 0;
    Ball ball;
    List<Point2D.Double> points;
    public Line(){
        this.setOpaque(false);
    }

    public void reDraw(Ball ball,BackgroundPanel backgroundPanel,int stepX,int stepY,boolean isControlDown){
        this.ball = ball;
        this.stepX = stepX;
        this.stepY = stepY;
        int width = backgroundPanel.getWidth();
        int height = backgroundPanel.getHeight()-115;
        this.setPreferredSize(new Dimension(width,height));
        this.setBounds(0,55+stepY,width,height);
        startY = ball.getY();
        startX = ball.getX();
        endX = getWidth()/2+stepX/3;
        endY = 0;
        if(isControlDown) {
            this.setVisible(false);
            ball.setBounds(ball.getX() + stepX, ball.getY(), ball.getWidth(), ball.getHeight());
        }else{
            this.setVisible(true);
        }
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//        CubicCurve2D quadCurve2D = new CubicCurve2D.Double(startX,startY, startX-step,startY-30, endX+step, endY, endX, endY);
        // 二次曲线
        QuadCurve2D quadCurve2D = new QuadCurve2D.Double(startX,startY, startX+stepX,endY+stepY+50, endX, endY+stepY);
        // 通过二次曲线，生成曲线上的点
        PathIterator pi = quadCurve2D.getPathIterator(g2d.getTransform(),6);// 从二次曲线中取出特征点
        points = new ArrayList<>(25);
        while (!pi.isDone()) {
            double[] coords = new double[6];
            switch (pi.currentSegment(coords)) {
                case PathIterator.SEG_MOVETO:
                case PathIterator.SEG_LINETO:
                    points.add(new Point2D.Double(coords[0], coords[1]));
                    break;
            }
            pi.next();
        }
        g2d.setColor(Color.WHITE);
        Point2D.Double point = null;
        for (Point2D.Double temp : points) {
            point = temp;
            g2d.fillOval((int) point.x, (int) point.y,10,10);//在每个特征点上画一个小圆圈
        }
//        g2d.drawRect(0,0,getWidth(),getHeight());
    }

    public List<Point2D.Double> getPoints() {
        return points;
    }

}
